/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TCPPanel.java
 *
 * Created on 2010-01-06, 13:06:36
 */

package efe.gui;

import efe.bean.Efe16bitNumberValue;
import efe.bean.Efe32bitNumberValue;
import efe.bean.EfeBooleanValue;
import efe.bean.EfePortValue;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Vector;
import jpcap.packet.TCPPacket;

/**
 * Panel z nagłówkiem protokołu TCP.
 * @author Paweł
 */
public class TCPPanel extends javax.swing.JPanel implements Serializable {

    /** Creates new form TCPPanel */
    public TCPPanel() {
        initComponents();
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        sourcePortValue = new efe.bean.EfePortValue();
        destinationPortValue = new efe.bean.EfePortValue();
        sequenceNumberValue = new efe.bean.Efe32bitNumberValue();
        acknowledgmentNumberValue = new efe.bean.Efe32bitNumberValue();
        cwrValue = new efe.bean.EfeBooleanValue();
        eceValue = new efe.bean.EfeBooleanValue();
        urgValue = new efe.bean.EfeBooleanValue();
        ackValue = new efe.bean.EfeBooleanValue();
        pshValue = new efe.bean.EfeBooleanValue();
        rstValue = new efe.bean.EfeBooleanValue();
        synValue = new efe.bean.EfeBooleanValue();
        finValue = new efe.bean.EfeBooleanValue();
        windowValue = new efe.bean.Efe16bitNumberValue();
        urgentPointerValue = new efe.bean.Efe16bitNumberValue();
        jPanel1 = new javax.swing.JPanel();
        sourcePortField = new efe.bean.EfeEditorField();
        destinationPortField = new efe.bean.EfeEditorField();
        jPanel2 = new javax.swing.JPanel();
        sequenceNumberField = new efe.bean.EfeEditorField();
        jPanel3 = new javax.swing.JPanel();
        acknowledgmentNumberField = new efe.bean.EfeEditorField();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        dataOffsetField = new efe.bean.EfeEditorBlankField();
        reservedField = new efe.bean.EfeEditorBlankField();
        jPanel9 = new javax.swing.JPanel();
        cwrField = new efe.bean.EfeEditorField();
        eceField = new efe.bean.EfeEditorField();
        urgField = new efe.bean.EfeEditorField();
        ackField = new efe.bean.EfeEditorField();
        pshField = new efe.bean.EfeEditorField();
        rstField = new efe.bean.EfeEditorField();
        synField = new efe.bean.EfeEditorField();
        finField = new efe.bean.EfeEditorField();
        windowField = new efe.bean.EfeEditorField();
        jPanel6 = new javax.swing.JPanel();
        efeEditorBlankField3 = new efe.bean.EfeEditorBlankField();
        urgentPointerField = new efe.bean.EfeEditorField();
        jPanel7 = new javax.swing.JPanel();
        efeEditorBlankField4 = new efe.bean.EfeEditorBlankField();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setMaximumSize(new java.awt.Dimension(620, 32767));
        setMinimumSize(new java.awt.Dimension(620, 39));
        setPreferredSize(new java.awt.Dimension(620, 300));
        setLayout(new java.awt.GridLayout(6, 1));

        jPanel1.setLayout(new java.awt.GridLayout(1, 2));

        sourcePortField.setText("Source port");
        sourcePortField.setLabelText("Source port");
        sourcePortField.setValue(sourcePortValue);
        jPanel1.add(sourcePortField);

        destinationPortField.setText("Destination port");
        destinationPortField.setLabelText("Destination port");
        destinationPortField.setValue(destinationPortValue);
        jPanel1.add(destinationPortField);

        add(jPanel1);

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        sequenceNumberField.setText("Sequence number");
        sequenceNumberField.setLabelText("Sequence number");
        sequenceNumberField.setValue(sequenceNumberValue);
        jPanel2.add(sequenceNumberField);

        add(jPanel2);

        jPanel3.setLayout(new java.awt.GridLayout(1, 0));

        acknowledgmentNumberField.setText("Acknowledgment number");
        acknowledgmentNumberField.setLabelText("Acknowledgment number");
        acknowledgmentNumberField.setValue(acknowledgmentNumberValue);
        jPanel3.add(acknowledgmentNumberField);

        add(jPanel3);

        jPanel4.setLayout(new java.awt.GridLayout(1, 0));

        jPanel5.setLayout(new java.awt.GridLayout(1, 0));

        jPanel8.setLayout(new java.awt.GridLayout(1, 0));

        dataOffsetField.setText("Data offset");
        jPanel8.add(dataOffsetField);

        reservedField.setText("Reserved");
        jPanel8.add(reservedField);

        jPanel5.add(jPanel8);

        jPanel9.setLayout(new java.awt.GridLayout(1, 8));

        cwrField.setText("CWR");
        cwrField.setFont(new java.awt.Font("DejaVu Sans", 0, 10));
        cwrField.setLabelText("CWR");
        cwrField.setValue(cwrValue);
        jPanel9.add(cwrField);

        eceField.setText("ECE");
        eceField.setFont(new java.awt.Font("DejaVu Sans", 0, 10));
        eceField.setLabelText("ECE");
        eceField.setValue(eceValue);
        jPanel9.add(eceField);

        urgField.setText("URG");
        urgField.setFont(new java.awt.Font("DejaVu Sans", 0, 10));
        urgField.setLabelText("URG");
        urgField.setValue(urgValue);
        jPanel9.add(urgField);

        ackField.setText("ACK");
        ackField.setFont(new java.awt.Font("DejaVu Sans", 0, 10));
        ackField.setLabelText("ACK");
        ackField.setValue(ackValue);
        jPanel9.add(ackField);

        pshField.setText("PSH");
        pshField.setFont(new java.awt.Font("DejaVu Sans", 0, 10));
        pshField.setLabelText("PSH");
        pshField.setValue(pshValue);
        jPanel9.add(pshField);

        rstField.setText("RST");
        rstField.setFont(new java.awt.Font("DejaVu Sans", 0, 10));
        rstField.setLabelText("RST");
        rstField.setValue(rstValue);
        jPanel9.add(rstField);

        synField.setText("SYN");
        synField.setFont(new java.awt.Font("DejaVu Sans", 0, 10));
        synField.setLabelText("SYN");
        synField.setValue(synValue);
        jPanel9.add(synField);

        finField.setText("FIN");
        finField.setFont(new java.awt.Font("DejaVu Sans", 0, 10));
        finField.setLabelText("FIN");
        finField.setValue(finValue);
        jPanel9.add(finField);

        jPanel5.add(jPanel9);

        jPanel4.add(jPanel5);

        windowField.setText("Window");
        windowField.setToolTipText("label");
        windowField.setLabelText("Window");
        windowField.setValue(windowValue);
        jPanel4.add(windowField);

        add(jPanel4);

        jPanel6.setLayout(new java.awt.GridLayout(1, 0));

        efeEditorBlankField3.setText("Checksum");
        jPanel6.add(efeEditorBlankField3);

        urgentPointerField.setText("UrgentPointer");
        urgentPointerField.setLabelText("UrgentPointer");
        urgentPointerField.setValue(urgentPointerValue);
        jPanel6.add(urgentPointerField);

        add(jPanel6);

        jPanel7.setLayout(new java.awt.GridLayout(1, 0));

        efeEditorBlankField4.setText("Options");
        jPanel7.add(efeEditorBlankField4);

        add(jPanel7);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Tworzy pakiet TCP z pól nagłówka, gotowy do tego żeby osadzić go
     * w warstwie sieciowej.
     * @return pakiet
     */
    public jpcap.packet.TCPPacket getTCPPacket() {
        //TCPPacket(int src_port, int dst_port,
        //          long sequence, long ack_num,
        //          boolean urg, boolean ack, boolean psh,
        //          boolean rst, boolean syn, boolean fin,
        //          boolean rsv1, boolean rsv2,
        //          int window, int urgent)
        return 
            new TCPPacket(sourcePortValue.getValue(), destinationPortValue.getValue(),
                          sequenceNumberValue.getValue(), acknowledgmentNumberValue.getValue(), 
                          urgValue.getValue(), ackValue.getValue(), pshValue.getValue(),
                          rstValue.getValue(), synValue.getValue(), finValue.getValue(),
                          // TODO: reserverd1, reserved2
                          true, true,
                          windowValue.getValue(), urgentPointerValue.getValue());
    }

    /**
     * Zwraca wektor serializowalnych wartości, które należy zapisać do pliku.
     * @return kolekcja wartości nagłówka
     */
    public Vector<Object> getValuesVector() {
        Vector<Object> v = new Vector<Object>();   
        v.add(ackValue);
        v.add(acknowledgmentNumberValue);
        v.add(cwrValue);
        v.add(destinationPortValue);
        v.add(eceValue);
        v.add(finValue);
        v.add(pshValue);
        v.add(rstValue);
        v.add(sequenceNumberValue);
        v.add(sourcePortValue);
        v.add(synValue);
        v.add(urgValue);
        v.add(urgentPointerValue);
        v.add(windowValue);
        return v;
    }

    /**
     * Zapisuje dane z wektora do pól nagłówka.
     * @param v wektor wartości z elementami nagłówka w kolejności takiej samej
     * jak ta zwrócona przez metodę getValuesVector()
     */
    public void setValuesFromVector(Vector<Object> v) {
        Iterator<Object> i = v.iterator();
        ackValue = (EfeBooleanValue) i.next();
        ackField.setValue(ackValue);
        acknowledgmentNumberValue = (Efe32bitNumberValue) i.next();
        acknowledgmentNumberField.setValue(acknowledgmentNumberValue);
        cwrValue = (EfeBooleanValue) i.next();
        cwrField.setValue(cwrValue);
        destinationPortValue = (EfePortValue) i.next();
        destinationPortField.setValue(destinationPortValue);
        eceValue = (EfeBooleanValue) i.next();
        eceField.setValue(eceValue);
        finValue = (EfeBooleanValue) i.next();
        finField.setValue(finValue);
        pshValue = (EfeBooleanValue) i.next();
        pshField.setValue(pshValue);
        rstValue = (EfeBooleanValue) i.next();
        rstField.setValue(rstValue);
        sequenceNumberValue = (Efe32bitNumberValue) i.next();
        sequenceNumberField.setValue(sequenceNumberValue);
        sourcePortValue = (EfePortValue) i.next();
        sourcePortField.setValue(sourcePortValue);
        synValue = (EfeBooleanValue) i.next();
        synField.setValue(synValue);
        urgValue = (EfeBooleanValue) i.next();
        urgField.setValue(urgValue);
        urgentPointerValue = (Efe16bitNumberValue) i.next();
        urgentPointerField.setValue(urgentPointerValue);
        windowValue = (Efe16bitNumberValue) i.next();
        windowField.setValue(windowValue);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private efe.bean.EfeEditorField ackField;
    private efe.bean.EfeBooleanValue ackValue;
    private efe.bean.EfeEditorField acknowledgmentNumberField;
    private efe.bean.Efe32bitNumberValue acknowledgmentNumberValue;
    private efe.bean.EfeEditorField cwrField;
    private efe.bean.EfeBooleanValue cwrValue;
    private efe.bean.EfeEditorBlankField dataOffsetField;
    private efe.bean.EfeEditorField destinationPortField;
    private efe.bean.EfePortValue destinationPortValue;
    private efe.bean.EfeEditorField eceField;
    private efe.bean.EfeBooleanValue eceValue;
    private efe.bean.EfeEditorBlankField efeEditorBlankField3;
    private efe.bean.EfeEditorBlankField efeEditorBlankField4;
    private efe.bean.EfeEditorField finField;
    private efe.bean.EfeBooleanValue finValue;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private efe.bean.EfeEditorField pshField;
    private efe.bean.EfeBooleanValue pshValue;
    private efe.bean.EfeEditorBlankField reservedField;
    private efe.bean.EfeEditorField rstField;
    private efe.bean.EfeBooleanValue rstValue;
    private efe.bean.EfeEditorField sequenceNumberField;
    private efe.bean.Efe32bitNumberValue sequenceNumberValue;
    private efe.bean.EfeEditorField sourcePortField;
    private efe.bean.EfePortValue sourcePortValue;
    private efe.bean.EfeEditorField synField;
    private efe.bean.EfeBooleanValue synValue;
    private efe.bean.EfeEditorField urgField;
    private efe.bean.EfeBooleanValue urgValue;
    private efe.bean.EfeEditorField urgentPointerField;
    private efe.bean.Efe16bitNumberValue urgentPointerValue;
    private efe.bean.EfeEditorField windowField;
    private efe.bean.Efe16bitNumberValue windowValue;
    // End of variables declaration//GEN-END:variables

}
